"""
Case Type   : cm_install
Case Name   : cm_install执行前，集群各节点的互信有问题
Create At   : 2025/4/17
Owner       : @ningyali
Description :
    1.删除数据库节点间的互信
    2.配置xml文件中的cm相关参数
    3.数据库用户下执行cm_install
    4.sshfile文件中放置要互信的节点ip,创建互信
Expect      :
    1.执行成功
    2.执行成功
    3.安装成功
    4.执行成功
History     :
"""
import os
import time
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMInstallCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.constant = Constant()
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        self.create_cmd = f'source {macro.DB_ENV_PATH};' \
                          f'echo -e "{self.user_nodes[0].ssh_host}\n' \
                          f'{self.user_nodes[1].ssh_host}\n' \
                          f'{self.user_nodes[2].ssh_host}\n" >> tmp_hosts;' \
                          f'echo {macro.COMMON_PASSWD}|gs_sshexkey -f tmp_hosts;' \
                          f'rm -rf tmp_hosts'

    def test_install(self):
        text = '----step1:删除数据库节点间的互信;expect:执行成功----'
        self.log.info(text)
        rm_cmd = f"rm -rf ~/.ssh"
        rm_res = self.common.get_sh_result(self.user_nodes[0], rm_cmd)
        self.assertNotIn('error', rm_res.lower(), f'执行失败: {text}')

        text = '----step2:配置xml文件中的cm相关参数;expect:执行成功----'
        self.log.info(text)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        text = '----step3:数据库用户下执行cm_install;;expect:安装成功----'
        self.log.info(text)
        install_res = self.cm.cm_install(self.user_nodes[0])
        self.assertNotIn('Install CM tool success', install_res, '执行失败:' + text)

        text = '----step4:sshfile文件中放置要互信的节点ip,创建互信;expect:执行成功----'
        self.log.info(text)
        create_res = self.common.get_sh_result(
            self.user_nodes[0], self.create_cmd)
        self.assertIn(
            "Successfully created SSH trust", create_res, "执行失败:" + text)

    def tearDown(self):
        text = '----poststep1:还原xml;expect:执行成功----'
        self.log.info(text)
        sed_cmd = f'sed -i \'/cm/d\' {self.cm.xml_path};'
        sed_res = self.common.get_sh_result(self.user_nodes[0], sed_cmd)

        status = self.pri_sh.get_db_cluster_status()
        if not status:
            self.log.info('意外场景导致未恢复互信时，此处恢复互信')
            create_res = self.common.get_sh_result(self.user_nodes[0],
                                                   self.create_cmd)
            self.assertIn("Successfully created SSH trust", create_res,
                          "执行失败:" + text)
        self.assertNotIn('error', sed_res.lower(), "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
